package com.easysoft.framework.friendlink.service;

import com.easysoft.commons.utils.UUIDUtils;
import com.easysoft.framework.annotation.jdbc.DataSource;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.friendlink.mapper.FriendLinkMapperExtd;
import com.easysoft.framework.friendlink.model.FriendLinkView;
import com.easysoft.framework.friendlink.model.FriendLinkQuery;
import com.easysoft.framework.friendlink.model.SysFriendLink;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("service.FriendLinkService")
public class FriendLinkService {

    @Autowired
    @Qualifier("mapper.FriendLinkMapperExtd")
    private FriendLinkMapperExtd friendLinkMapperExtd;

    /**
     * 根据id查询友情链接
     *
     * @param id id
     */
    @DataSource(DataSource.SLAVE)
    public SysFriendLink selectFriendLinkById(String id) {
        return friendLinkMapperExtd.selectByPrimaryKey(id);
    }

    /**
     * 获取友情链接列表
     *
     * @param page  分页参数
     * @param query 查询条件
     * @return 友情链接的List集合
     */
    @DataSource(DataSource.SLAVE)
    public List<FriendLinkView> selectSysFriendListByPage(Page page, FriendLinkQuery query) {
        int maxRow = friendLinkMapperExtd.selectCountAllFriendLink(query);
        if (maxRow == 0) {
            return new ArrayList<FriendLinkView>();
        }
        page.setMaxRow(maxRow);
        return friendLinkMapperExtd.selectSysFriendListByPage(page, query);
    }

    public List<Map<String,String>> selectSysFriendLink(){
        return friendLinkMapperExtd.selectSysFriendLink();
    }


    @DataSource(DataSource.MASTER)
    public int addFriendLink(SysFriendLink sysFriendLink) {
        sysFriendLink.setId(UUIDUtils.getUUID());
        sysFriendLink.setCreateTime(new Date());
        return friendLinkMapperExtd.insertSelective(sysFriendLink);
    }

    @DataSource(DataSource.MASTER)
    public int deleteFriendLink(String id) {
        return friendLinkMapperExtd.deleteByPrimaryKey(id);
    }

    @DataSource(DataSource.MASTER)
    public int updateFriendLink(SysFriendLink sysFriendLink) {
        return friendLinkMapperExtd.updateByPrimaryKeySelective(sysFriendLink);
    }
}
